-
Notifications
You must be signed in to change notification settings - Fork 150
Fix a crash when reporting an error at the end of a file #310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The compiler crashes with a segmentation fault when an unterminated C-style comment exists at the very end of a file. The root cause is a buffer over-read in the error() function, which attempts to construct a diagnostic message by reading the source line containing the error. When the error is on the last line of a file without a trailing newline, this logic would read past the end of the source buffer. Fix the issue by adding a bounds check to the loop, ensuring it does not read beyond the source buffer's size. This allows the compiler to correctly report the "Unenclosed C-style comment" error instead of crashing.
|
FWIW, this issue can be easily reproduced with the following example: int main() {
/* this is an unterminated comment
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 issue found across 1 file
Prompt for AI agents (all 1 issues)
Understand the root cause of the following 1 issues and fix them.
<file name="src/globals.c">
<violation number="1" location="src/globals.c:1398">
This loop initializes `offset` to `SOURCE->size`, which causes an out-of-bounds read on the first iteration when accessing `SOURCE->elements[offset]`. The loop should initialize `offset` to `SOURCE->size - 1` to access the last valid element.</violation>
</file>
React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai to give feedback, ask questions, or re-run the review.
|
|
||
| for (offset = 0; | ||
| offset < MAX_SOURCE && SOURCE->elements[start_idx + offset] != '\n'; | ||
| offset < MAX_SOURCE && (start_idx + offset) < SOURCE->size && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This loop initializes offset to SOURCE->size, which causes an out-of-bounds read on the first iteration when accessing SOURCE->elements[offset]. The loop should initialize offset to SOURCE->size - 1 to access the last valid element.
Prompt for AI agents
Address the following comment on src/globals.c at line 1398:
<comment>This loop initializes `offset` to `SOURCE->size`, which causes an out-of-bounds read on the first iteration when accessing `SOURCE->elements[offset]`. The loop should initialize `offset` to `SOURCE->size - 1` to access the last valid element.</comment>
<file context>
@@ -1395,7 +1395,8 @@ void error(char *msg)
for (offset = 0;
- offset < MAX_SOURCE && SOURCE->elements[start_idx + offset] != '\n';
+ offset < MAX_SOURCE && (start_idx + offset) < SOURCE->size &&
+ SOURCE->elements[start_idx + offset] != '\n';
offset++) {
</file context>
|
Thank @visitorckw for contributing! |
The compiler crashes with a segmentation fault when an unterminated C-style comment exists at the very end of a file.
The root cause is a buffer over-read in the error() function, which attempts to construct a diagnostic message by reading the source line containing the error. When the error is on the last line of a file without a trailing newline, this logic would read past the end of the source buffer.
Fix the issue by adding a bounds check to the loop, ensuring it does not read beyond the source buffer's size. This allows the compiler to correctly report the "Unenclosed C-style comment" error instead of crashing.
Summary by cubic
Prevents a crash when reporting an error at EOF by adding a source-bounds check while building the diagnostic line. Unterminated C-style comments at the end of a file without a trailing newline now produce a proper error instead of a segfault.